﻿using Biotech.Utils;
using MaterialDesignThemes.Wpf;
using project.B12.IService;
using project.B12.Model.Common;
using project.B12.Model.Enums;
using project.B12.Model.Res;
using project.B12.Service.UserService;
using software.Common.MVVM;
using System;
using System.Collections.Generic;

namespace project.B12.application.ViewModels.Dialogs
{
    public class ExpirationTimeViewModel : ObservableObject
    {
        #region 构造函数
        public ExpirationTimeViewModel(IUserService userService)
        {
            UserService = userService;
            ExpirationLists = EnumUtil.GetEnumDisplay<ExpirationTimeEnum>(typeof(ExpirationTimeEnum));//有效期下拉框清单
        }
        #endregion

        #region Model属性
        public IUserService UserService { get; set; }//用户服务入口
        private string userName;//用户名
        private DateTime currentExpirationTime;//当前过期时间
        private ExpirationTimeEnum? expirationTime;//新的过期时间
        private string expirationTimeTips;//新的过期时间提示
        private Dictionary<string, ExpirationTimeEnum> expirationLists { get; set; } = new Dictionary<string, ExpirationTimeEnum>();//过期策略清单
        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }
        public DateTime CurrentExpirationTime
        {
            get { return currentExpirationTime; }
            set { currentExpirationTime = value; RaisePropertyChanged(); }
        }
        public ExpirationTimeEnum? ExpirationTime
        {
            get { return expirationTime; }
            set { expirationTime = value; RaisePropertyChanged(); }
        }
        public string ExpirationTimeTips
        {
            get { return expirationTimeTips; }
            set { expirationTimeTips = value; RaisePropertyChanged(); }
        }
        public Dictionary<string, ExpirationTimeEnum> ExpirationLists
        {
            get { return expirationLists; }
            set { expirationLists = value; RaisePropertyChanged(); }
        }
        #endregion

        #region Command命令
        public RelayCommand SaveExpirationTimeCommand => new RelayCommand(SaveExpirationTime);//保存过期时间
        public RelayCommand ExitViewCommand => new RelayCommand(ExitView);//退出窗体
        #endregion

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="obj"></param>
        private void SaveExpirationTime()
        {
            System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
                try
                {
                    if (DialogHost.IsDialogOpen("Main"))
                    {
                        if (ExpirationTime == null)
                        {
                            ExpirationTimeTips = Resources.ExpirationTimeEmpty;
                            return;
                        }
                        DialogHost.Close("Main", ExpirationTime);//成功
                    }
                }
                catch (Exception ex)
                {
                    ExpirationTimeTips = ex.Message;
                }
            });
        }

        /// <summary>
        /// 退出窗体
        /// </summary>
        /// <param name="obj"></param>
        private void ExitView()
        {
            if (DialogHost.IsDialogOpen("Main"))
                DialogHost.Close("Main", null);
        }
    }
}
